
#include "Ddx_game.h"
#include "SaveSoundInfo.h"
#include "../g2/SaveFilePath.h"
//#include "system.h"

extern void   ChangePath(char *pDesFilePath,char  *pFileName);

BOOL ReadInfSound(char SoundFileName[], pINFSOUND pSoundInfo ) ;
void SaveInfSound(char SoundFileName[], pINFSOUND pSoundInfo ) ;


#if 1
BOOL ReadInfSound(char SoundFileName[], pINFSOUND pSoundInfo ) 
{
	HANDLE	filehandle ;
	DWORD 	filelength ;
	DWORD	reallength ;

	char  		filepath[128]	;
	TCHAR	File[128]	;

	//ChangePath(filepath, SoundFileName);
	ChangeSavePath(filepath, SoundFileName);
	GetUnicodeString(File, (LPCSTR)filepath, 0 );
	printf("%s \n",filepath);
	filehandle =CreateFile((LPCTSTR)File, GENERIC_READ,FILE_SHARE_READ,
				(LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY, (HANDLE)NULL);

	if(filehandle==INVALID_HANDLE_VALUE)
	{
		printf("Open file lost in ReadSoundInfo \r\n");
		return FALSE ;
	}
	
	filelength = sizeof(INFSOUND) ;

	ReadFile(filehandle, pSoundInfo, filelength , &reallength,NULL) ;
	CloseFile(filehandle ) ;

	return TRUE ;
}

void SaveInfSound(char SoundFileName[], pINFSOUND pSoundInfo ) 
{
	HANDLE	filehandle ;

	TCHAR      File[128];
	DWORD 	filelength ;

	DWORD	reallength ;

	char  	filepath[128]	;
	//ChangePath(filepath, SoundFileName)	;
	ChangeSavePath(filepath, SoundFileName);
	GetUnicodeString(File, (LPCSTR)filepath, 0);
	printf("%s \n",filepath);
	filehandle =CreateFile((LPCTSTR)File,  GENERIC_WRITE,  FILE_SHARE_WRITE, NULL
		, CREATE_NEW , FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
	if(filehandle==INVALID_HANDLE_VALUE)
	{
		
		printf("Open file lost in SaveSoundInfo \r\n");
		return;
	}
	filelength = sizeof(INFSOUND) ;
	WriteFile(filehandle, pSoundInfo, filelength , &reallength,NULL) ;
	CloseFile( filehandle  ) ;

}
#endif



void SetSoundCfg(char SoundFileName[], PDWORD pBKSoundVolume,PDWORD pEffectSoundVolume)
{
	printf("111 pBKSoundVolume:%d, pEffectSoundVolume:%d \n", *pBKSoundVolume, *pEffectSoundVolume);
	INFSOUND SoundInfo;
	BOOL bReadRut ; 
	bReadRut = ReadInfSound(SoundFileName, &SoundInfo) ;
	if(bReadRut)
	{
		*pBKSoundVolume=SoundInfo.BKSoundVolume;
		*pEffectSoundVolume=SoundInfo.EffectSoundVolume;
		GM_SetPrimaryVolume( SoundInfo.PrimaryVolume);
	}
	
	printf("222 pBKSoundVolume:%d, pEffectSoundVolume:%d \n", *pBKSoundVolume, *pEffectSoundVolume);
}

void SaveSoundCfg(char SoundFileName[], DWORD BKSoundVolume,DWORD EffectSoundVolume)
{
	printf("333 BKSoundVolume:%d, EffectSoundVolume:%d \n", BKSoundVolume, EffectSoundVolume);
	INFSOUND SoundInfo;
	SoundInfo.BKSoundVolume=BKSoundVolume;
	SoundInfo.EffectSoundVolume=EffectSoundVolume;
	GM_GetPrimaryVolume( &(SoundInfo.PrimaryVolume));
	SaveInfSound(SoundFileName, &SoundInfo ) ;
}



